<#include "/lib/lib.html">
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Bing-UPMS 后台权限管理</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="${request.contextPath}/favicon.ico">
    <link href="${request.contextPath}/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="${request.contextPath}/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="${request.contextPath}/css/animate.css" rel="stylesheet">
    <link href="${request.contextPath}/css/style.css?v=4.1.0" rel="stylesheet">
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.css">
    <link rel="stylesheet" href="${request.contextPath}/css/plugins/bootstrap-validator/bootstrapValidator.min.css">
    <link rel="stylesheet" href="${request.contextPath}/plugins/treegrid/jquery.treegrid.css">
    <style>
        .form-control:focus,
        .single-line:focus {
            border-color: #0e9aef !important;
        }
    </style>
</head>
<body class="gray-bg">

    <div class="wrapper wrapper-content">
        <div class="ibox float-e-margins ibox-blue">
            <div class="ibox-title">
                <h5>权限管理</h5>
                <div class="ibox-tools">
                    <a class="collapse-link">
                        <i class="fa fa-chevron-up"></i>
                    </a>
                    <a class="close-link">
                        <i class="fa fa-times"></i>
                    </a>
                </div>
            </div>
            <div class="ibox-content">

                <div class="row row-lg">
                    <div class="example" style="margin-top: -10px;">
                        <div class="btn-group hidden-xs" id="toolbar" role="group">
                            <button type="button" class="btn btn-outline btn-default" data-toggle="modal" data-target="#addModal">
                                <i class="glyphicon glyphicon-plus" aria-hidden="true"></i>
                                添加
                            </button>
                            <button type="button" class="btn btn-outline btn-default" id="updateButton">
                                <i class="glyphicon glyphicon-pencil" aria-hidden="true"></i>
                                修改
                            </button>
                            <button type="button" class="btn btn-outline btn-default" id="deleteButton">
                                <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                                删除
                            </button>
                        </div>

                        <table id="menuTable" data-mobile-responsive="true" data-click-to-select="true">
                            <thead>
                            <tr>
                                <th data-field="selectItem" data-checkbox="true"></th>
                            </tr>
                            </thead>
                        </table>

                    </div>
                </div>

            </div>
        </div>
    </div>


    <@modal "添加权限" "addModal" "addForm">
    <div class="form-group">
        <label class="col-sm-2 control-label">类型</label>
        <div class="col-sm-10" id="typeRadio">
            <label>
                <input type="radio" name="type" value="0" checked="">
                <span>目录</span>
            </label>
            <label>
                <input type="radio" name="type" value="1">
                <span>菜单</span>
            </label>
            <label>
                <input type="radio" name="type" value="2">
                <span>按钮</span>
            </label>
        </div>
    </div>
    <div class="form-group pid">
        <label class="col-sm-2 control-label">上级目录</label>
        <div class="col-sm-10">
            <select class="form-control" name="pid">
                <option>11</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">权限名称</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="name" placeholder="请输入权限名称">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">图标</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="icon" placeholder="请输入图标图标">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">排序</label>
        <div class="col-sm-10">
            <input type="number" class="form-control" name="sort" placeholder="请输入排序">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">权限编码</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="permCode" placeholder="请输入权限编码">
        </div>
    </div>
    <div class="form-group url">
        <label class="col-sm-2 control-label">url</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="url" placeholder="请输入url">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">描述</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="description" placeholder="请输入描述">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">状态</label>
        <div class="col-sm-10">
            <div class="ui-radio ui-radio-primary">
                <label>
                    <input type="radio" name="status" value="1" checked="">
                    <span>启用</span>
                </label>
                <label>
                    <input type="radio" name="status" value="0">
                    <span>禁用</span>
                </label>
            </div>
        </div>
    </div>
    </@modal>

    <@modal "修改权限" "updateModal" "updateForm">
    <input type="hidden" name="id">
    <div class="form-group">
        <label class="col-sm-2 control-label">类型</label>
        <div class="col-sm-10" id="typeRadio2">
            <label>
                <input type="radio" name="type" value="0" checked="">
                <span>目录</span>
            </label>
            <label>
                <input type="radio" name="type" value="1">
                <span>菜单</span>
            </label>
            <label>
                <input type="radio" name="type" value="2">
                <span>按钮</span>
            </label>
        </div>
    </div>
    <div class="form-group pid">
        <label class="col-sm-2 control-label">上级菜单</label>
        <div class="col-sm-10">
            <select class="form-control" id="pid" name="pid">
                <option>1</option>
                <option>2</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">权限名称</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="name" placeholder="请输入权限名称">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">图标</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="icon" placeholder="请输入图标图标">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">排序</label>
        <div class="col-sm-10">
            <input type="number" class="form-control" name="sort" placeholder="请输入排序">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">权限编码</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="permCode" placeholder="请输入权限编码">
        </div>
    </div>
    <div class="form-group url">
        <label class="col-sm-2 control-label">url</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="url" placeholder="请输入url">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">描述</label>
        <div class="col-sm-10">
            <input type="text" class="form-control" name="description" placeholder="请输入描述">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">状态</label>
        <div class="col-sm-10">
            <div class="ui-radio ui-radio-primary">
                <label>
                    <input type="radio" name="status" value="1" checked="">
                    <span>启用</span>
                </label>
                <label>
                    <input type="radio" name="status" value="0">
                    <span>禁用</span>
                </label>
            </div>
        </div>
    </div>
    </@modal>

    <!-- 全局js -->
    <script src="${request.contextPath}/js/jquery.min.js?v=2.1.4"></script>
    <script src="${request.contextPath}/js/bootstrap.min.js?v=3.3.6"></script>
    <!-- layer javascript -->
    <script src="${request.contextPath}/js/plugins/layer/layer.min.js"></script>
    <!-- 自定义js -->
    <script src="${request.contextPath}/js/content.js?v=1.0.0"></script>
    <!-- Latest compiled and minified JavaScript -->
    <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.js"></script>
    <!-- Latest compiled and minified Locales -->
    <script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/locale/bootstrap-table-zh-CN.min.js"></script>
    <script src="${request.contextPath}/js/plugins/bootstrap-validator/bootstrapValidator.js"></script>
    <!-- treegrid -->
    <script src="${request.contextPath}/plugins/treegrid/jquery.treegrid.min.js"></script>
    <script src="${request.contextPath}/plugins/treegrid/jquery.treegrid.bootstrap3.js"></script>
    <script src="${request.contextPath}/plugins/treegrid/jquery.treegrid.extension.js"></script>
    <script src="${request.contextPath}/plugins/treegrid/tree.table.js"></script>

    <script>
        var Menu = {
            id: "menuTable",
            table: null,
            layerIndex: -1
        };

        /**
         * 初始化表格的列
         */
        Menu.initColumn = function () {
            var columns = [
                {
                    field: 'selectItem',
                    radio: true
                },
                {
                    field: 'id',
                    title: 'ID',
                    align: 'center',
                    valign: 'middle',
                    width: '60px'
                },
                {
                    field: 'name',
                    title: '权限名称',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    field: 'icon',
                    title: '图标',
                    align: 'center',
                    valign: 'middle',
                    formatter: function(item, index) {
                        return item.icon == null ? '' : '<i class="'+item.icon+' fa-lg"></i>';
                    }
                },
                {
                    field: 'type',
                    title: '类型',
                    align: 'center',
                    valign: 'middle',
                    formatter: function(item, index) {
                        if(item.type === 0){
                            return '<span class="label label-primary">目录</span>';
                        }
                        if(item.type === 1){
                            return '<span class="label label-success">菜单</span>';
                        }
                        if(item.type === 2){
                            return '<span class="label label-warning">按钮</span>';
                        }
                    }
                },
                {
                    field: 'sort',
                    title: '排序号',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    field: 'url',
                    title: '权限URL',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    field: 'permCode',
                    title: '权限编码',
                    align: 'center',
                    valign: 'middle'
                },
                {
                    field: 'status',
                    title: '状态',
                    align: 'center',
                    valign: 'middle',
                    formatter: function(item, index){
                        if (item.status == 0) {
                            return '<span class="label label-danger">禁用</span>';
                        } else if (item.status == 1) {
                            return '<span class="label label-primary">启用</span>';
                        }
                        return "<span>其他</span>";
                    }
                },
                {
                    field: 'gmtCreate',
                    title: '创建时间',
                    align: 'center',
                    formatter:function(item, index){
                        return formatDateTime(item.gmtCreate);
                    }
                }
                ];
            return columns;
        };

        $(function () {
            var colunms = Menu.initColumn();
            var table = new TreeTable(Menu.id, "/admin/permission/getList", colunms);
            table.setExpandColumn(2);
            table.setIdField("id");
            table.setCodeField("id");
            table.setParentCodeField("pid");
            table.setExpandAll(false);
            table.init();
            Menu.table = table;
        });

        function getMenuId () {
            var selected = $('#menuTable').bootstrapTreeTable('getSelections');
            if (selected.length == 0) {
                parent.layer.msg('请选择一条记录', {time: 1500, icon:5});
                return false;
            } else {
                return selected[0].id;
            }
        }

        //添加权限
        $('#addModal').on('show.bs.modal', function () {
            $("#addForm").data('bootstrapValidator').destroy();
            $('#addForm').data('bootstrapValidator', null);
            addValidator();
        });
        addValidator();
        function addValidator() {
            $('#addForm').bootstrapValidator({
                fields: {
                    name: {
                        validators: {
                            notEmpty: {
                                message: '权限名称不能为空'
                            }
                        }
                    },
                    icon: {
                        validators: {
                            notEmpty: {
                                message: '图标不能为空'
                            },
                            stringLength: {
                                max: 30,
                                message: '图标长度最多15个字符'
                            }
                        }
                    },
                    sort: {
                        validators: {
                            notEmpty: {
                                message: '排序不能为空'
                            }
                        }
                    },
                    permCode: {
                        validators: {
                            notEmpty: {
                                message: '权限编码不能为空'
                            }
                        }
                    },
                    url: {
                        validators: {
                            notEmpty: {
                                message: 'url不能为空'
                            }
                        }
                    },
                    description: {
                        validators: {
                            notEmpty: {
                                message: '描述不能为空'
                            }
                        }
                    }
                }
            }).on('success.form.bv', function(e) {
                // Prevent form submission
                e.preventDefault();
                $.ajax({
                    url: "/admin/permission/addPermission",
                    data:  $("#addForm").serialize(),
                    type: "POST",
                    dataType: 'json',
                    success: function(data){
                        $("#addModal").modal('hide');
                        if (data.success) {
                            $("#addModal").modal('hide');
                            $("#addForm")[0].reset();
                            $("#addModal .pid").hide();
                            Menu.table.refresh();
                            parent.layer.msg(data.msg, {icon: 1});
                        } else {
                            parent.layer.msg(data.msg, {time: 1500, icon:5});
                        }
                    }
                });
            });
        }

        //修改权限
        $("#updateButton").click(function () {
            var id = getMenuId();
            if (id == false) {
                return false;
            }
            list = [];
            $.ajax({
                async:false,
                url: "/admin/permission/getPermissionById",
                data: {"id": id},
                type: "POST",
                dataType: 'json',
                success: function(data){
                    list = data;
                }
            });
            for (var item in list) {
                if (item == 'status' || item == 'type') {
                    continue;
                }
                $("#updateModal input[name='"+item+"']").val(list[item]);
            }
            if (list.type == 0) {
                $("#updateModal .pid").hide();
            } else if (list.type == 1) {
                $("#updateModal .pid").show();
                setDomPermistion(0);
            } else if (list.type == 2) {
                $("#updateModal .pid").show();
                setDomPermistion(1);
            }
            $("#updateModal input[name='type'][value="+list.type+"]").prop("checked",true);
            console.log(list.type)
            $("#updateModal input[name='status'][value="+list.status+"]").prop("checked",true);
            $("#updateForm").data('bootstrapValidator').destroy();
            $('#updateForm').data('bootstrapValidator', null);
            updateValidator();
            $("#updateModal").modal("show");
        });
        updateValidator();
        function updateValidator() {
            $('#updateForm').bootstrapValidator({
                fields: {
                    name: {
                        validators: {
                            notEmpty: {
                                message: '权限名称不能为空'
                            }
                        }
                    },
                    icon: {
                        validators: {
                            notEmpty: {
                                message: '图标不能为空'
                            },
                            stringLength: {
                                max: 30,
                                message: '图标长度最多15个字符'
                            }
                        }
                    },
                    sort: {
                        validators: {
                            notEmpty: {
                                message: '排序不能为空'
                            }
                        }
                    },
                    permCode: {
                        validators: {
                            notEmpty: {
                                message: '权限编码不能为空'
                            }
                        }
                    },
                    url: {
                        validators: {
                            notEmpty: {
                                message: 'url不能为空'
                            }
                        }
                    },
                    description: {
                        validators: {
                            notEmpty: {
                                message: '描述不能为空'
                            }
                        }
                    }
                }
            }).on('success.form.bv', function(e) {
                // Prevent form submission
                e.preventDefault();
                $.ajax({
                    url: "/admin/permission/updatePermission",
                    data:  $("#updateForm").serialize(),
                    type: "POST",
                    dataType: 'json',
                    success: function(data){
                        $("#updateModal").modal('hide');
                        if (data.success) {
                            $("#updateModal").modal('hide');
                            Menu.table.refresh();
                            parent.layer.msg(data.msg, {icon: 1});
                        } else {
                            parent.layer.msg(data.msg, {time: 1500, icon:5});
                        }
                    }
                });
            });
        }

        $("#deleteButton").click(function () {
            var id = getMenuId();
            parent.layer.confirm('确定删除选中权限？', {
                btn: ['确定','取消'], //按钮
                shade: false //不显示遮罩
            }, function() {
                $.ajax({
                    url: "/admin/permission/deletePermissionRole",
                    data:  {"id": id},
                    type: "POST",
                    dataType: 'json',
                    success: function(data){
                        if (data.success) {
                            $('#userTableEvents').bootstrapTable('refresh');
                            parent.layer.msg(data.msg, {icon: 1});
                            Menu.table.refresh();
                        } else {
                            parent.layer.msg(data.msg, {time: 1500, icon:5});
                        }
                    }
                });
            });
        });

        $("#addModal .pid").hide();
        $("#addModal #typeRadio input:radio").click(function () {
            if (this.value == 0) {
                $("#addModal .pid").hide();
            } else if (this.value == 1) {
                setDomPermistion(0);
                $("#addModal .pid").show();
            } else if (this.value == 2) {
                setDomPermistion(1);
                $("#addModal .pid").show();
            }
        });


        $("#updateModal #typeRadio2 input:radio").click(function () {
            if (this.value == 0) {
                $("#updateModal .pid").hide();
            } else if (this.value == 1) {
                setDomPermistion(0);
                $("#updateModal .pid").show();
            } else if (this.value == 2) {
                setDomPermistion(1);
                $("#updateModal .pid").show();
            }
        });

        function setDomPermistion(type) {
            $("#addModal select, #updateModal select").empty();
            if (type == 0) {
                $.get("/admin/permission/getTypePermission/0", function(result){
                    var domOption = "";
                    for (var item in result) {
                        domOption += "<option value='"+result[item].id+"'>"+result[item].name+"</option>";
                    }
                    $("#addModal select, #updateModal select").append(domOption);
                    $("#updateModal #pid").val(list.pid);
                });
            } else if (type == 1) {
                $.get("/admin/permission/getTypePermission/1", function(result){
                    var domOption = "";
                    for (var item in result) {
                        domOption += "<option value='"+result[item].id+"'>"+result[item].name+"</option>";
                    }
                    $("#addModal select, #updateModal select").append(domOption);
                    $("#updateModal #pid").val(list.pid);
                });
            }
        }
    </script>
</body>
</html>
